import pygame
from pygame.rect import Rect


def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 600), 0, 32)

    # 画出三条颜色的线，高度80，每条一个颜色，颜色值从0-255
    red_scale, green_scale, blue_scale = create_scales(80)

    color = [127, 127, 127]

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

        # 先来一个黑色背景填充窗口
        screen.fill((0, 0, 0))

        # 把画出的三条线的红色条，填充到屏幕上
        screen.blit(red_scale, (0, 00))
        screen.blit(green_scale, (0, 80))
        screen.blit(blue_scale, (0, 160))

        # 获取鼠标坐标
        x, y = pygame.mouse.get_pos()

        # 鼠标点击后获取，鼠标所在位置的颜色值，0-255
        if pygame.mouse.get_pressed()[0]:
            for commpent in range(3):
                if y > commpent * 80 and y < (commpent + 1) * 80:
                    color[commpent] = int((x / 800.) * 255.)

        # 标题显示选定的颜色值
        pygame.display.set_caption("pygame color test -" + str(tuple(color)))

        # 画三个圈，
        for commpent in range(3):
            pos = (int((color[commpent] / 255.) * 800), commpent * 80 + 40) # 圈的中心点位置
            pygame.draw.circle(screen, (255, 255, 255), pos, 20)  # Surface, color, pos, radius, width=0

        # 画一个(0, 240, 640, 240) 位置的矩形来显示选择的三条线后的颜色。
        pygame.draw.rect(screen, tuple(color), (0, 240, 640, 240)) # Surface, color, Rect, width=0

        pygame.display.update()


def create_scales(height):
    red_scale_surface = pygame.Surface((800, height))
    green_scale_surface = pygame.Surface((800, height))
    blue_scale_surface = pygame.Surface((800, height))

    for x in range(800):
        c = int((x / 800.) * 255.)
        red = (c, 0, 0)
        green = (0, c, 0)
        blue = (0, 0, c)

        # left, top, width, height
        line_rect = Rect(x, 0, 1, height)
        pygame.draw.rect(red_scale_surface, red, line_rect)
        pygame.draw.rect(green_scale_surface, green, line_rect)
        pygame.draw.rect(blue_scale_surface, blue, line_rect)

    return red_scale_surface, green_scale_surface, blue_scale_surface


main()
